package com.hzeport.bgs22emduty.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.login.LoginCompoentService;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.dto.ScreenServiceGuanranteeDTO;
import com.hzeport.bgs22emduty.dto.ScreenServiceGuanranteeSaveDTO;
import com.hzeport.bgs22emduty.outbound.ScreenServiceGuanranteeRepository;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import com.hzeport.bgs22emduty.entity.ScreenServiceGuanrantee;
import com.hzeport.bgs22emduty.outbound.db.ScreenServiceGuanranteeRepositoryImpl;
import com.hzeport.bgs22emduty.service.ScreenServiceGuanranteeService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2023-02-14 09:39:44
 */
@Service
@Slf4j
public class ScreenServiceGuanranteeServiceImpl extends BaseServiceImpl<ScreenServiceGuanranteeRepositoryImpl, ScreenServiceGuanrantee> implements ScreenServiceGuanranteeService {

    @Resource
    private ScreenServiceGuanranteeRepository screenServiceGuanranteeRepository;

    @Resource
    private LoginCompoentService loginCompoentService;

    @Override
    public Object getScreenServiceGuanranteePage(ScreenServiceGuanranteeDTO dto) {
        UserInfoDetailVO userInfoDetailVO = loginCompoentService.getUserInfo();
        LambdaQueryWrapper<ScreenServiceGuanrantee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userInfoDetailVO.getCustomscode()),ScreenServiceGuanrantee::getCustomId,userInfoDetailVO.getCustomscode());
        if (StringUtils.isNotBlank(dto.getStartDate())){
            LocalDateTime startLocalDateTime = LocalDate.parse(dto.getStartDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
            queryWrapper.ge(ScreenServiceGuanrantee::getCreateTime,startLocalDateTime);
        }
        if (StringUtils.isNotBlank(dto.getEndDate())){
            LocalDateTime endLocalDateTime = LocalDate.parse(dto.getEndDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay().plusDays(1);
            queryWrapper.le(ScreenServiceGuanrantee::getCreateTime,endLocalDateTime);
        }
        queryWrapper.orderByDesc(ScreenServiceGuanrantee::getCreateTime);
        Page<ScreenServiceGuanrantee> page = screenServiceGuanranteeRepository.page(new Page<>(dto.getPageNum(), dto.getPageSize()), queryWrapper);
        return page;
    }

    @Override
    public Boolean save(ScreenServiceGuanranteeSaveDTO dto) {
        UserInfoDetailVO userInfoDetailVO = loginCompoentService.getUserInfo();
        ScreenServiceGuanrantee screenServiceGuanrantee = new ScreenServiceGuanrantee();
        BeanUtil.copyProperties(dto,screenServiceGuanrantee);
        screenServiceGuanrantee.setCustomId(userInfoDetailVO.getCustomscode());
        screenServiceGuanrantee.setCustomName(userInfoDetailVO.getCustomsName());
        screenServiceGuanrantee.setCreatePerson(userInfoDetailVO.getUserName());
        return screenServiceGuanranteeRepository.save(screenServiceGuanrantee);
    }
}
